set more off
clear all

*global rootfolder ""
global WIODfolder "$rootfolder\data\3_WIOD"
global outputfolder "$rootfolder\figures\temp"
global chartfolder "$rootfolder\figures\charts"

capture mkdir "$rootfolder\figures"
capture mkdir "$outputfolder"
capture mkdir "$chartfolder"

cd "$chartfolder"

global ctry AUS AUT BEL BGR CAN CHN CZE DEU DNK ESP FIN FRA GBR GRC HUN IRL ITA JPN KOR LTU MEX NLD POL PRT ROM ROW RUS SVN SWE TWN USA
global Srest_candidates "50 51 52 h 60 61 62 63 64 j 70 71t74 l m n o p f" 

**************************************************************************************** 
*** Country level
use "$WIODfolder\WIOD.dta", clear
gen ke_partner = 0
gen ke_country = 0
gen ke_cind = 0
gen ke_pind = 0


foreach cc in $ctry {
	replace ke_partner = 1 if partner == "`cc'"
	replace ke_country = 1 if country == "`cc'"
}

foreach ss in $Srest_candidates {
	replace ke_cind = 1 if country_ind == "`ss'"
	replace ke_pind = 1 if partner_ind == "`ss'"
}

replace country = "ROW" if ke_country == 0
replace partner = "ROW" if ke_partner == 0

preserve
*save a version with all sectors
collapse (sum) inter_use, by(country partner year)
				
save "$outputfolder\temp_1.dta", replace

restore,preserve
	
*save a version with only non-services
drop if ke_cind == 1
drop if ke_pind == 1
	
collapse (sum) inter_use, by(country partner year)
	
save "$outputfolder\temp_2.dta", replace

restore,preserve 
*save a version with all sectors
collapse (sum) inter_use, by(country_ind country partner year)
	
save "$outputfolder\temp_sector_1.dta", replace
	
restore
*save a version with only non-services
drop if ke_cind == 1
drop if ke_pind == 1

collapse (sum) inter_use, by(country_ind country partner year)
	
save "$outputfolder\temp_sector_2.dta", replace


********************************************************************************
* Figure 1 
use "$outputfolder\temp_1.dta", clear

gen d_foreign = .
replace d_foreign = 1 if country != partner
replace d_foreign = 0 if country == partner
tabulate d_for

*generate foreign/domestic input 
gen foreign_inp = d_foreign*inter_use
gen domestic_inp = (1-d_foreign)*inter_use

*collapse
collapse (sum) inter_use foreign_inp domestic_inp, by(country)

gen total_foreign_share_all = foreign_inp/inter_use
gen total_domestic_share_all = domestic_inp/inter_use
rename inter_use inter_use_all

save "$outputfolder\temp_country_share_allsectors.dta", replace

**With only non-service sectors
use "$outputfolder\temp_2.dta", clear

gen d_foreign = .
replace d_foreign = 1 if country != partner
replace d_foreign = 0 if country == partner
tabulate d_for

*generate foreign/domestic input 
gen foreign_inp = d_foreign*inter_use
gen domestic_inp = (1-d_foreign)*inter_use

*collapse
collapse (sum) inter_use foreign_inp domestic_inp, by(country)

gen total_foreign_share_noserv = foreign_inp/inter_use
gen total_domestic_share_noserv = domestic_inp/inter_use
rename inter_use inter_use_noserv

merge 1:1 country using "$outputfolder\temp_country_share_allsectors.dta"

egen tmp_rank = rank(-total_foreign_share_noserv) 
gen rank = tmp_rank
sort rank
labmask rank, values(country)
graph bar total_foreign_share_noserv, over(rank,lab(angle(vertical))) ytitle("Share") ///
	legend(off) ///
	title("") graphregion(fcolor(white) lcolor(white))
graph export "$chartfolder\fig1_foreign_input_share_byctry_noserv.eps", replace as(eps)
graph export "$chartfolder\fig1_foreign_input_share_byctry_noserv.pdf", replace as(pdf)

*******************************************************************************
* Figure 2
use "$outputfolder\temp_sector_1.dta", clear

gen d_foreign = .
replace d_foreign = 1 if country != partner
replace d_foreign = 0 if country == partner
tabulate d_for

gen total_foreign_inp = d_foreign*inter_use
gen total_domestic_inp = (1-d_foreign)*inter_use

collapse (sum) inter_use total_foreign_inp total_domestic_inp, by(country_ind)

gen total_foreign_share_all = total_foreign_inp/inter_use
gen total_domestic_share_all = total_domestic_inp/inter_use
rename inter_use inter_use_all

save "$outputfolder\temp_sector_share_allsectors.dta", replace

**With only non-service sectors
use "$outputfolder\temp_sector_2.dta", clear

gen d_foreign = .
replace d_foreign = 1 if country != partner
replace d_foreign = 0 if country == partner
tabulate d_for

gen total_foreign_inp = d_foreign*inter_use
gen total_domestic_inp = (1-d_foreign)*inter_use

collapse (sum) inter_use total_foreign_inp total_domestic_inp, by(country_ind)

gen total_foreign_share_noserv = total_foreign_inp/inter_use
gen total_domestic_share_noserv = total_domestic_inp/inter_use
rename inter_use inter_use_noserv

merge 1:1 country_ind using "$outputfolder\temp_sector_share_allsectors.dta"

gen names = "."
gen labels = "."
replace  names = "Food, Beverages and Tobacco" if country_ind == "15t16"
replace labels = "Food, Bev. & Tobacco" if country_ind == "15t16"
replace  names = "Textiles and Textile Products" if country_ind == "17t18"
replace labels = "Textiles" if country_ind == "17t18"
replace  names = "Leather, Leather and Footwear" if country_ind == "19"
replace labels = "Leather & Footwear" if country_ind == "19"
replace  names = "Wood and Products of Wood and Cork" if country_ind == "20"
replace labels = "Wood Products" if country_ind == "20"
replace  names = "Pulp, Paper, Paper, Printing and Publishing" if country_ind == "21t22"
replace labels = "Pulp, Paper, Print." if country_ind == "21t22"
replace  names = "Coke, Refined Petroleum and Nuclear Fuel" if country_ind == "23"
replace labels = "Coke, Ref. Petrol." if country_ind == "23"
replace  names = "Chemicals and Chemical Products" if country_ind == "24"
replace labels = "Chemical products" if country_ind == "24"
replace  names = "Rubber and Plastics" if country_ind == "25"
replace labels = "Rubber & Plastics" if country_ind == "25"
replace  names = "Other Non-Metallic Mineral" if country_ind == "26"
replace labels = "Other Non-Metal. Mineral" if country_ind == "26"
replace  names = "Basic Metals and Fabricated Metal" if country_ind == "27t28"
replace labels = "Basic & Fabric. Metal" if country_ind == "27t28"
replace  names = "Machinery, Nec" if country_ind == "29"
replace labels = "Machinery, Nec" if country_ind == "29"
replace  names = "Electrical and Optical Equipment" if country_ind == "30t33"
replace labels = "Electr. & Opt. Eqpmnt" if country_ind == "30t33"
replace  names = "Transport Equipment" if country_ind == "34t35"
replace labels = "Transp. Eqpmnt" if country_ind == "34t35"
replace  names = "Manufacturing, Nec; Recycling" if country_ind == "36t37"
replace labels = "Manufact., Nec" if country_ind == "36t37"
replace  names = "Sale, Maintenance and Repair of Motor Vehicles Retail Sale of Fuel" if country_ind == "50"
replace labels = "Vehicles Sales & Rep." if country_ind == "50"
replace  names = "Wholesale Trade and Commission Trade, Except of Motor Vehicles" if country_ind == "51"
replace labels = "Wholesale Trade" if country_ind == "51"
replace  names = "Retail Trade, Except of Motor Vehicles ; Repair of Household Goods" if country_ind == "52"
replace labels = "Retail Trade" if country_ind == "52"
replace  names = "Inland Transport" if country_ind == "60"
replace  labels = "Inland Transport" if country_ind == "60"
replace  names = "Water Transport" if country_ind == "61"
replace  labels = "Water Transport" if country_ind == "61"
replace  names = "Air Transport" if country_ind == "62"
replace  labels = "Air Transport" if country_ind == "62"
replace  names = "Other Supporting and Auxiliary Transport Activities; Activities of Travel Agencies" if country_ind == "63"
replace  labels = "Aux. Transp. Act." if country_ind == "63"
replace  names = "Post and Telecommunications" if country_ind == "64"
replace  labels = "Post & Telec." if country_ind == "64"
replace  names = "Real Estate Activities" if country_ind == "70"
replace  labels = "Real Estate" if country_ind == "70"
replace  names = "Renting of M&Eq and Other Business Activities" if country_ind == "71t74"
replace  labels = "Renting of M&Eq, Other" if country_ind == "71t74"
replace  names = "Agriculture, Hunting, Forestry and Fishing" if country_ind == "atb"
replace  labels = "Agriculture" if country_ind == "atb"
replace  names = "Mining and Quarrying" if country_ind == "c"
replace  labels = "Mining & Quarrying" if country_ind == "c"
replace  names = "Electricity, Gas and Water Supply" if country_ind == "e"
replace  labels = "Energy & Water Supply" if country_ind == "e"
replace  names = "Construction" if country_ind == "f"
replace  labels = "Construction" if country_ind == "f"
replace  names = "Hotels and Restaurants" if country_ind == "h"
replace  labels = "Hotels & Rest." if country_ind == "h"
replace  names = "Financial Intermediation" if country_ind == "j"
replace  labels = "Fin. Intermed." if country_ind == "j"
replace  names = "Public Admin and Defence; Compulsory Social Security" if country_ind == "l"
replace  labels = "Pub. Adm. and Defence" if country_ind == "l"
replace  names = "Education" if country_ind == "m"
replace  labels = "Education" if country_ind == "m"
replace  names = "Health and Social Work" if country_ind == "n"
replace  labels = "Health & Social Work" if country_ind == "n"
replace  names = "Other Community, Social and Personal Services" if country_ind == "o"
replace  labels = "Other Soc. Services" if country_ind == "o"
replace  names = "Private Households with Employed Persons" if country_ind == "p"
replace  labels = "Private Households" if country_ind == "p"

gen industrycode = country_ind
replace industrycode = "AtB" if country_ind=="atb"
replace industrycode = "C" if country_ind=="c"
replace industrycode = "E" if country_ind=="e"
replace industrycode = "F" if country_ind=="f"


gen names_short =   abbrev(names,19)

egen tmp_rank = rank(-total_foreign_share_noserv) 
gen rank = tmp_rank
sort rank
labmask rank, values(labels)

graph bar total_foreign_share_noserv, over(rank,lab(angle(50) labsize(small))) ///
	legend(off) ///
	ytitle("share") title("") graphregion(fcolor(white) lcolor(white))
graph export "$chartfolder\fig2_foreign_input_share_bysectr_noserv.eps", replace as(eps)
graph export "$chartfolder\fig2_foreign_input_share_bysectr_noserv.pdf", replace as(pdf)

*******************************************************************************
* Figure 3
use "$outputfolder\temp_1.dta", clear

gen d_foreign = .
replace d_foreign = 1 if country != partner
replace d_foreign = 0 if country == partner
tabulate d_for

*generate foreign/domestic input 
gen foreign_inp = d_foreign*inter_use
gen domestic_inp = (1-d_foreign)*inter_use

*collapse
collapse (sum) inter_use foreign_inp domestic_inp, by(year)

gen total_foreign_share_all = foreign_inp/inter_use
gen total_domestic_share_all = domestic_inp/inter_use
rename inter_use inter_use_all

save "$outputfolder\temp_tot_share_allsectors.dta", replace

**With only non-service sectors
use "$outputfolder\temp_2.dta", clear

gen d_foreign = .
replace d_foreign = 1 if country != partner
replace d_foreign = 0 if country == partner
tabulate d_for

*generate foreign/domestic input 
gen foreign_inp = d_foreign*inter_use
gen domestic_inp = (1-d_foreign)*inter_use

*collapse
collapse (sum) inter_use foreign_inp domestic_inp, by(year)

gen total_foreign_share_noserv = foreign_inp/inter_use
gen total_domestic_share_noserv = domestic_inp/inter_use
rename inter_use inter_use_noserv

merge 1:1 year using "$outputfolder\temp_tot_share_allsectors.dta"

tsset year
line total_foreign_share_noserv year,  title("") ytitle("Share") ///
	legend(off) graphregion(fcolor(white) lcolor(white))
graph export "$chartfolder\fig3_foreign_input_share_byyear_noserv.eps", replace as(eps)
graph export "$chartfolder\fig3_foreign_input_share_byyear_noserv.pdf", replace as(pdf)

* For the next script
cd "$rootfolder\figures"
